package model.factories.misc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;
import model.Interpreter;
import model.SyntaxErrorException;
import model.commands.Command;
import model.commands.misc.UserDefinedCommand;
import model.factories.CommandFactory;


/**
 * 
 * @author Erick Gonzalez
 */
public class UserDefinedCommandFactory extends CommandFactory {

    /**
     * 
     * @param interpreter an interpreter
     */
    public UserDefinedCommandFactory(Interpreter interpreter) {
        super(interpreter);
    }

    @Override
    public Pattern patternToMatch() {
        Interpreter interpreter = getInterpreter();
        Set<String> userDefinedCommandNames =
                interpreter.getUserDefinedCommandNames();
        StringBuilder patternBuilder = new StringBuilder();
        for (String name : userDefinedCommandNames) {
            patternBuilder.append(name + "|");
        }
        return Pattern.compile(patternBuilder.toString());
    }

    @Override
    public Command createCommand(Scanner commandReader,
                                 Map<String, Integer> variables) throws SyntaxErrorException {
        Interpreter interpreter = getInterpreter();        

        String commandName = commandReader.next();
        String commandInfoString = interpreter.getCommandInfo(commandName);
        String parameterString = getParameterString(commandInfoString);
        String commandString = getCommandString(commandInfoString);
        List<String> parameters = getParameters(parameterString);        
        Map<String, Integer> newVariables = new HashMap<String, Integer>(variables);
                  
        Command userDefinedCommand = new UserDefinedCommand(interpreter,
                                                            newVariables,
                                                            commandString,
                                                            parameters);        
        
        List<Command> commandArguments = getCommandArguments(variables, 
                                                             interpreter, 
                                                             commandReader, 
                                                             parameters.size());
        userDefinedCommand.getArguments().addAll(commandArguments);

        return userDefinedCommand;
    }
    
    
    private String getParameterString(String commandInfoString) {
        return commandInfoString.substring(commandInfoString.indexOf('[') + 1,
                                            commandInfoString.indexOf(']')).trim();
    }
    
    private String getCommandString(String commandInfoString) {
        int commandBracketIndex =
                commandInfoString.indexOf('[', commandInfoString.indexOf(']'));
        return commandInfoString.substring(commandBracketIndex,
                                   commandInfoString.lastIndexOf(']') + 1);
    }

    private List<Command> getCommandArguments(Map<String, Integer> variables,
                                                 Interpreter interpreter,
                                                 Scanner commandReader,
                                                 int numArgs) throws SyntaxErrorException {
        List<Command> commands = new ArrayList<Command>();
        for (int i = 0; i < numArgs; ++i) {
            commands.add(interpreter.parse(commandReader, variables));
        }
        
        return commands;
    }
    
    
    
    private List<String> getParameters(String parameterString) {
        String[] splitString = parameterString.split("\\s");
        return Arrays.asList(splitString);
    }
}
